package com.rgs.base.core.model;

import cn.hutool.core.lang.Pair;
import com.rgs.base.core.system.WebResponseState;
import com.rgs.base.core.util.RgsJson;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author Ren Gaoshuai
 * @ClassName WebApiReturnResultModel
 * @Description 统一的controller返回模型
 * @date 2021-12-24 16:18
 **/
@Data
public class WebApiReturnResultModel<T> {
    private int code;
    private String msg;
    private T data;

    public WebApiReturnResultModel() {
        super();
    }

    public WebApiReturnResultModel(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    //  @Override
    public Boolean resultSuccess() {
        return code >= 200 && code < 300;
    }

    @Override
    public String toString() {
        return RgsJson.toJSONString(this);
    }


    public static WebApiReturnResultModel<Void> ofSuccess() {
        return ofSuccess(null);
    }

    /**
     * 定义常用静态方法
     */
    public static <T> WebApiReturnResultModel<T> ofSuccess(T data) {
        return new WebApiReturnResultModel<>(WebResponseState.SUCCESS.getCode(),
                WebResponseState.SUCCESS.getStandardMessage(), data);
    }

    public static <T> WebApiReturnResultModel<String> ofSuccessJson(T data) {
        String strJson = RgsJson.toJSONString(data);
        return new WebApiReturnResultModel<>(WebResponseState.SUCCESS.getCode(),
                WebResponseState.SUCCESS.getStandardMessage(), strJson);
    }

//    public static WebApiReturnResultModel ofSuccessToJson(
//            WebApiReturnResultModel webApiReturnResultModel) {
//
//        String strJson = JSON.toJSONString(webApiReturnResultModel.getData());
//        return new WebApiReturnResultModel(WebResponseState.SUCCESS.getCode(),
//                WebResponseState.SUCCESS.getStandardMessage(), strJson);
//    }

    public static WebApiReturnResultModel<Void> ofMessage(int code, String message) {
        return new WebApiReturnResultModel<>(code, message, null);
    }

    public static <T> WebApiReturnResultModel<T> ofMessage(int code, String message, T data) {
        return new WebApiReturnResultModel<>(code, message, data);
    }
    public static <T> WebApiReturnResultModel<T> of(WebResponseState status, String message, T data) {
        return new WebApiReturnResultModel<>(status.getCode(), message, data);
    }
    public static WebApiReturnResultModel<Void> ofStatus(WebResponseState status) {
        return new WebApiReturnResultModel<>(status.getCode(), status.getStandardMessage(), null);
    }

    public static WebApiReturnResultModel<Void> ofStatus(WebResponseState status, String msg) {
        return new WebApiReturnResultModel<>(status.getCode(), msg, null);
    }

    public static <T> WebApiReturnResultModel<T> ofSuccess(WebResponseState status, T data) {
        return new WebApiReturnResultModel<>(status.getCode(), status.getStandardMessage(), data);
    }

    public static WebApiReturnResultModel<Map<String, Object>> ofSuccessPair(Pair<String, Object>... objectPairArray) {
        Map<String, Object> map = new HashMap<>(objectPairArray.length);
        for (Pair<String, Object> pairTemp : objectPairArray) {
            map.put(pairTemp.getKey(), pairTemp.getValue());
        }
        return ofSuccess(map);
    }

//    public static WebApiReturnResultModel ofErr(WuXiHuaJieCommonException ex) {
//        return WebApiReturnResultModel.ofMessage(ex.getCode(), ex.getMsg());
//    }
//    public static WebApiReturnResultModel ofErr(WuXiHuaJieFeignError ex) {
//        return WebApiReturnResultModel.ofMessage(ex.getWebApiReturnResultModel().getCode(), ex.getWebApiReturnResultModel().getMsg());
//    }


    /**
     * 返回文件名或转到异步任务列表
     *
     */
    public static WebApiReturnResultModel<Void> ofFuture(Future<WebApiReturnResultModel<Void>> future) {
        return ofFuture(future, 2);
    }

    public static WebApiReturnResultModel<Void> ofFuture(Future<WebApiReturnResultModel<Void>> future,
                                                         Integer maxWaitTime,
                                                         WebResponseState webResponseState
    ) {
        try {
            return future.get(maxWaitTime, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException e) {
            return WebApiReturnResultModel.ofStatus(WebResponseState.ASYNC_TASK_ERROR, e.getMessage());
        } catch (TimeoutException e) {
            return WebApiReturnResultModel.ofStatus(webResponseState);
        }
    }

    /**
     * 返回文件名或转到异步任务列表
     */
    public static WebApiReturnResultModel<Void> ofFuture(Future<WebApiReturnResultModel<Void>> future,
                                                         Integer maxWaitTime) {
        return WebApiReturnResultModel.ofFuture(future, maxWaitTime, WebResponseState.SUCCESS_TO_TASK);
    }
}

